package com.ilop.sthome.ui.adapter.scene;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-12-12.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：自动化条件选择
 */
public class ChoseDeviceAdapter extends RecyclerView.Adapter<ChoseDeviceAdapter.ItemHolder> {

    private Context mContext;
    private List<DeviceInfoBean> mLists;

    public ChoseDeviceAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setLists(List<DeviceInfoBean> mLists) {
        this.mLists = mLists;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_add_sub_device, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DeviceInfoBean device = mLists.get(i);
        if (!TextUtils.isEmpty(device.getProductKey())){
            itemHolder.mImg.setImageResource(R.mipmap.gs198);
            String gatewayName = mContext.getString(R.string.ali_gateway);
            itemHolder.mName.setText(TextUtils.isEmpty(device.getNickName()) ? gatewayName : device.getNickName());
        }else {
            itemHolder.mImg.setImageResource(SmartDevice.getType(device.getDevice_type()).getDrawableResId());
            if (TextUtils.isEmpty(device.getSubdeviceName())) {
                SmartDevice mSmartDevice = SmartDevice.getType(device.getDevice_type());
                String deviceType = mContext.getString(mSmartDevice.getTypeStrId());
                switch (mSmartDevice){
                    case EE_SIMULATE_GATEWAY:
                    case EE_SIMULATE_CLICK:
                    case EE_SIMULATE_PHONE:
                    case EE_SIMULATE_DELAY:
                    case EE_SIMULATE_TIMER:
                        itemHolder.mName.setText(deviceType);
                        break;
                    default:
                        String name = deviceType + device.getDevice_ID();
                        itemHolder.mName.setText(name);
                        break;
                }
            } else {
                itemHolder.mName.setText(device.getSubdeviceName());
            }
        }
        itemHolder.itemView.setOnClickListener(v -> LiveDataBus.get().with(CommonId.CHOOSE_CONDITION).setValue(device));
    }

    @Override
    public int getItemCount() {
        return mLists == null ? 0 : mLists.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.productImg)
        ImageView mImg;
        @BindView(R.id.productName)
        TextView mName;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
